<template>
    <div class="main">
        <div class="top">
            <div class="breadcrumb">
                <p class="toptitle">首页</p>
                <i class="iconfont icon-youjiantou"></i>
                <p class="toptitle">账号管理</p>
                <i class="iconfont icon-youjiantou"></i>
                <p class="lasttitle">个人中心</p>
            </div>
            <Welcome></Welcome>
        </div>
        <div class="table">
            <p class="list">个人中心</p>
            <div class="msg" v-if="newdata">
                <p class="name">管理员: {{ newdata.id }}</p>
                <p class="account">账号: {{ newdata.account }}</p>
                <p class="ru">用户组: {{ newdata.userGroup }}</p>
                <p class="time">创建时间: {{ newdata.createTime }}</p>
                <p class="img">用户头像: </p>
                <el-upload class="avatar-uploader"
                    :action=" '/api/users/avatar_upload?id=' + newdata.id" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="newdata.avatar" :src="serverUrl + newdata.avatar" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { getaccountinfo, imguploadaccount } from '../../api/account.js';
import { ref } from 'vue';
import dayjs from 'dayjs';
import { serverUrl } from "../../utils/commen.js"
import Welcome from '../../components/Welcome.vue';
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'

// 格式化时间
const formatDate = (date) => {
    return dayjs(date).format('YYYY-MM-DD HH:mm:ss'); // 使用 dayjs 格式化时间
};

// 定义一个响应式对象，用于存储个人信息
const newdata = ref({
    id: 1,
    account: null,
    userGroup: null,
    createTime: null,
    avatar: null
});

// 定义一个函数，用于获取个人信息
const fetchAccountInfo = async (msg) => {
    const data = await getaccountinfo(msg);
    newdata.value = data.accountInfo;
    data.accountInfo.createTime = formatDate(data.accountInfo.createTime);
    newdata.value.avatar = data.accountInfo.imgUrl; // 假设 imgUrl 是后端返回的头像路径
    console.log(data.accountInfo);
};

console.log(newdata.value);

// 在组件加载时调用 fetchAccountInfo 函数
fetchAccountInfo({ id: sessionStorage.getItem('accountid') });

//上传头像
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    // 假设后端返回的响应中包含图片路径，将其保存到 newdata 中
    newdata.value.avatar = response.imageUrl; // 这里的 imageUrl 是假设的后端返回的图片路径字段
    ElMessage.success('头像上传成功');
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}

</script>

<style scoped>
.main {
    width: 100%;
    height: 730px;
    background-color: #f0f0f0;
}

.table {
    margin: 20px;
    padding: 20px;
    border-radius: 5px;
    background-color: #fff;
    min-height: 600px;
}

.list {
    width: 100%;
    border-bottom: 1px solid #e1e1e1;
    font-size: 20px;
    margin-bottom: 20px;
    padding-bottom: 10px;
}

.top {
    width: 96%;
    height: 50px;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* 内容分布在左右两侧 */
    padding: 0 20px;
    /* 添加左右内边距 */
}

.breadcrumb {
    display: flex;
    align-items: center;
    font-size: 14px;
}

.iconfont {
    font-size: 14px;
    color: #999999;
    margin: 0 5px;
}
.toptitle {
  font-weight: bold;
}

.lasttitle {
    color: #999999;
}

.msg {
    margin-top: 20px;
    padding: 10px 0;
}

.name,
.account,
.ru,
.time {
    font-size: 18px;
    margin-bottom: 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e1e1e1;
}

/* 上传头像 */

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>